package com.example.hcd.adapt;

import java.util.ArrayList;

import com.example.hcd.R;
import com.example.hcd.datamanage.LoadedImage;
import com.example.hcd.internet.FileUtils;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/* 
 * Adapter 
 */
public class FileAdapter extends BaseAdapter {

    private Activity mContext;
    public ArrayList<LoadedImage> photos = new ArrayList<LoadedImage>();

    public FileAdapter(Activity context) {
        mContext = context;
    }

    public void addPhoto(LoadedImage photo) {
        photos.add(photo);
    }

    public int getCount() {
        return photos.size();
    }

    public LoadedImage getItem(int position) {
        return photos.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        if (rowView == null) {
            LayoutInflater inflater = mContext.getLayoutInflater();
            rowView = inflater.inflate(R.layout.griditem, null);

        } else {

        }
        ImageView imageView = (ImageView) rowView.findViewById(R.id.image);
        imageView.setImageBitmap(photos.get(position).getBitmap());
        TextView textView = (TextView) rowView.findViewById(R.id.text);

        String textName = getLast(photos.get(position).getPicPath());
        String text = "";
        if (FileUtils.isPic(textName)) {
            text = "photo" + getnum(true, position);
        } else {
            text = "file" + getnum(false, position) + getLast1(textName);
        }

        // Set the text on the TextView
//        TextView textView = viewCache.getTextView();
        textView.setText(text);

        //textView.setText("file"+(position+1));

        return rowView;
    }

    private int getnum(boolean ispng, int postion) {
        int num = 1;

        for (int i = 0; i < postion; i++) {
            if (ispng) {
                LoadedImage name = getItem(i);
                String textName = getLast(name.getPicPath());
                if (FileUtils.isPic(textName)) {
                    num++;
                }

            } else {
                LoadedImage name = getItem(i);
                String textName = getLast(name.getPicPath());
                if (FileUtils.isPic(textName)) {

                } else {
                    num++;
                }

            }


        }


        return num;
    }

    private String getLast(String path) {
        String a[] = path.split("/");
        if (a.length > 0) {
            return a[a.length - 1];
        } else {
            return path;
        }

    }

    private String getLast1(String path) {
        String a[] = path.split("\\.");
        if (a.length > 1) {
            return "." + a[a.length - 1];
        } else
            return "";

    }
}  
